var global_type=1;
var golbal_type1=1;
var inputObj;
var hideObj;
var globalClickObj;
var global_db_code;
var global_db_name;
var global_cl_code;
var global_cl_name;
var global_tm_code;
var global_tm_name;
var global_temp_id;
function getTree(type) {
	var setting = {
		async : {
			enable : true,
			url : "/FPM/Target/getTreeForNew?treeType="+ type
		},
		view : {
			dblClickExpand : false
		},
		data : {
			simpleData : {
				enable : true
			}
		},
		callback : {
			onClick : onClick
		}
	};
	$.fn.zTree.init($("#selectTree"), setting);
}



function onClick(e, treeId, treeNode) {
	if(global_type==1){
		if(treeNode.level!=2){
			alert("只能选择表!");
			return;
		}else{
			global_db_code.attr("value",treeNode.getParentNode().getParentNode().id.replace("db",""));
			global_db_name.attr("value",treeNode.getParentNode().getParentNode().name);
			global_cl_code.attr("value",treeNode.getParentNode().id.replace("cl",""));
			global_cl_name.attr("value",treeNode.getParentNode().name);
			var tmId = treeNode.id.replace("tb","");
			if(golbal_type1==1){
				$.ajax( {
		   			type : 'GET',
		   			url :"/FPM/Target/ajaxGetMaxCode?tmId="+tmId,
		   			cache : false,
		   			async: false,
		   			error : function(XMLHttpRequest, textStatus, errorThrown) {
		   				//alert(XMLHttpRequest.readyState);
		   			},
		   			success : function(data) {
		   				$("#target_id").val(data);
		   			}
		   		});
			}else{
				$.ajax( {
		   			type : 'GET',
		   			url :"/FPM/normType/ajaxGetMaxCode?tmId="+tmId,
		   			cache : false,
		   			async: false,
		   			error : function(XMLHttpRequest, textStatus, errorThrown) {
		   				//alert(XMLHttpRequest.readyState);
		   			},
		   			success : function(data) {
		   				$("#class_id").val(data);
		   			}
		   		});
			}
			
		}
	}else if(global_type==2){
		if(treeNode.level<3){
			alert("只能选择指标!");
			return;
		}else{
			$.ajax( {
	       			type : 'GET',
	       			url :"/FPM/Target/getDBCLTBbyTargetId?targetId="+treeNode.id,
	       			cache : false,
	       			async: false,
	       			error : function(XMLHttpRequest, textStatus, errorThrown) {
	       				//alert(XMLHttpRequest.readyState);
	       			},
	       			success : function(data) {
	       				global_db_code.attr("value",data.db_code);
						global_db_name.attr("value",data.db_name);
						global_cl_code.attr("value",data.cl_code);
						global_cl_name.attr("value",data.cl_name);
						global_tm_code.attr("value",data.tm_id);
						global_tm_name.attr("value",data.tm_name);
						global_temp_id.attr("value",data.target_id);
						$.ajax( {
				   			type : 'GET',
				   			url :"/FPM/Target/ajaxGetMaxCode?tmId="+data.tm_id+"&targetId="+data.target_id,
				   			cache : false,
				   			async: false,
				   			error : function(XMLHttpRequest, textStatus, errorThrown) {
				   				//alert(XMLHttpRequest.readyState);
				   			},
				   			success : function(data) {
				   				$("#target_id").val(data);
				   			}
				   		});
	       			}
	       		});
		}
	}else if(global_type==3){
		if(treeNode.level<3){
			alert("只能选择类别!");
			return;
		}else{
			$.ajax( {
	       			type : 'GET',
	       			url :"/FPM/Target/getDBbyClassId?classId="+treeNode.id,
	       			cache : false,
	       			async: false,
	       			error : function(XMLHttpRequest, textStatus, errorThrown) {
	       				//alert(XMLHttpRequest.readyState);
	       			},
	       			success : function(data) {
						global_tm_code.attr("value",data.tm_id);
						global_tm_name.attr("value",data.tm_name);
						global_temp_id.attr("value",data.class_id);
						$.ajax( {
				   			type : 'GET',
				   			url :"/FPM/normType/ajaxGetMaxCode?tmId="+data.tm_id+"&classId="+data.class_id,
				   			cache : false,
				   			async: false,
				   			error : function(XMLHttpRequest, textStatus, errorThrown) {
				   				//alert(XMLHttpRequest.readyState);
				   			},
				   			success : function(data) {
				   				$("#class_id").val(data);
				   			}
				   		});
	       			}
	       		});
		}
	}
	//globalClickObj.html("选择");
	inputObj.attr("value", treeNode.name);
	hideObj.attr("value",treeNode.id.replace("tb","").replace("tr","").replace("nt",""));
	hideMenu();
}

function showMenu(cityObj,hide,clickObj,type,db_code,db_name,cl_code,cl_name,tm_code,tm_name,temp_id,type1) {
	global_type = type;
	golbal_type1 = type1;
	global_db_code = db_code;
	global_db_name = db_name;
	global_cl_code = cl_code;
	global_cl_name = cl_name;
	global_tm_code = tm_code;
	global_tm_name = tm_name;
	global_temp_id = temp_id
	if($("#selectTreeDiv").is(":hidden")){
		//clickObj.html("关闭");
		$("#selectTree").empty();
		getTree(type);
		inputObj = cityObj;
		hideObj = hide;
		globalClickObj = clickObj
		var cityOffset = cityObj.offset();
		$("#selectTreeDiv").css({
			left : cityOffset.left + "px",
			top : cityOffset.top + cityObj.outerHeight() + "px"
		}).slideDown("fast");
	}else{
		//clickObj.html("选择");
		$("#selectTreeDiv").fadeOut("fast");
	}
	
}
function hideMenu() {
	$("#selectTreeDiv").fadeOut("fast");
}

function hasRight(allRights,right){
	for( var i=0;i<array.length;i++){
	  if( array[i] == str ){
		  return true;
	  }else{
		  return false;
	  }
	}
}